home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 July / july_2000.iso / Site Building / port scanner / scanSingleIP.java < prev    next >
Encoding:
Java Source  |  2000-06-07  |  3.2 KB  |  106 lines

  1. /*
  2.  * Java Port Scanner
  3.  *
  4.  * Copyright 2000 Matteo Baccan <mbaccan@planetisa.com>
  5.  * www - http://www.infomedia.it/artic/Baccan
  6.  *
  7.  * This program is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 2 of the License, or
  10.  * (at your option) any later version
  11.  *
  12.  * This program is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program; if not, write to the Free Software
  19.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA (or visit
  20.  * their web site at http://www.gnu.org/).
  21.  *
  22.  */
  23.  
  24. import java.net.*;
  25.  
  26. public class scanSingleIP extends Thread{
  27.    private String cIPToFind;
  28.    private int nPort;
  29.    private String cDesc;
  30.    private boolean bLog;
  31.    private int nTimeout;
  32.    private portScanner parent;
  33.  
  34.    public scanSingleIP( portScanner parent,
  35.                         String cIP,
  36.                         int nPort,
  37.                         String cDesc,
  38.                         boolean bLog ,
  39.                         int nTimeout ){
  40.       this.parent    = parent;
  41.       this.cIPToFind = cIP;
  42.       this.nPort     = nPort;
  43.       this.cDesc     = cDesc;
  44.       this.bLog      = bLog;
  45.       this.nTimeout  = nTimeout;
  46.    }
  47.  
  48.    public void run(){
  49.       while( true ){
  50.          try {
  51.             long nMils = System.currentTimeMillis();
  52.  
  53.             if( bLog )
  54.                log( cIPToFind +"(" +nPort +")" );
  55.  
  56.             Socket test = new Socket( cIPToFind, nPort );
  57.             nMils = System.currentTimeMillis()-nMils;
  58.  
  59.             String cReply = "";
  60.             try {
  61.                test.setSoTimeout( nTimeout );
  62.                int nChar = 0;
  63.                while( (nChar=test.getInputStream().read())>-1 )
  64.                   cReply += (char)nChar;
  65.             }catch( Throwable e ){
  66.                //log( e.toString() );
  67.                //e.printStackTrace();
  68.             }
  69.  
  70.             test.close();
  71.  
  72.             String cString2Ret  = "Found : " +cIPToFind +" " +cDesc;
  73.                    cString2Ret += "(" +new Integer(nPort).toString() +")";
  74.                    cString2Ret += " Millis(" +new Long(nMils).toString() +")\n";
  75.             if( cReply.length()>0 ){
  76.                cString2Ret += "Reply\n";
  77.                cString2Ret += cReply;
  78.             }else{
  79.                cString2Ret += "NoReply\n";
  80.             }
  81.  
  82.             log( cString2Ret );
  83.             break;
  84.          }catch(ConnectException e){
  85.             break;
  86.          }catch(SocketException e){
  87.             log( "Too many threads. Decrease the number of Thread" );
  88.             break;
  89.          }catch(Throwable e){
  90.             //log( e.toString() );
  91.             //e.printStackTrace();
  92.             break;
  93.          }
  94.       }
  95.       parent.threadDel();
  96.       stop();
  97.    }
  98.  
  99.    public void log( int nMsg ){
  100.       log( new Integer( nMsg ).toString() );
  101.    }
  102.    public void log( String cMsg ){
  103.       parent.log( cMsg );
  104.    }
  105. }
  106.